package com.lagou.mapper;

import com.lagou.pojo.User;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.cache.impl.PerpetualCache;

import java.util.List;

@CacheNamespace(implementation = PerpetualCache.class)//开启二级缓存
public interface IUserMapper {
    /**
     *  查询所有用户，同时查询每个用户关联的订单信息（一对多）
     */
    List<User> findAll();

    /**
     * 查询所有用户及其角色信息（多对多）
     */
    List<User> findUserAndRole();


    /**
     * 填加用户
     */
    @Insert("insert into user values(#{id},#{username})")
    void addUser(User user);

    /**
     * 更新用户
     * @param user
     */
    @Update("update user set username = #{username} where id = #{id}")
    void updateUser(User user);

    /**
     * 查询用户
     * @return
     */
    @Select("select * from user")
    List<User> selectUser();

    /**
     * 删除用户
     * @param id
     */
    @Delete("delete from user where id = #{id}")
    void deleteUser(int id);

    /**
     * 根据id查询用户()
     */
    @Select("select * from user where id = #{id}")
    User findUserById(int id);


    /**
     *  查询所有用户，同时查询每个用户关联的订单信息（一对多）
     */
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class, many = @Many(select = "com.lagou.mapper.IOrderMapper.findOrderByUid")),
    })
    @Select("select * from user")
    List<User> findAllByAnnotation();


    /**
     * 查询所有用户及其角色信息（多对多）
     */
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id", javaType = List.class, many = @Many(select = "com.lagou.mapper.IRoleMapper.findRoleByUid")),
    })
    @Select("select * from user")
    List<User> findUserAndRoleByAnnotation();


}
